package com.fcy.teamwork.business.recommendstudent

import android.content.Intent
import android.view.LayoutInflater
import android.view.ViewGroup
import android.widget.CheckBox
import com.fcy.teamwork.base.BaseRVAdapter
import com.fcy.teamwork.base.toJson
import com.fcy.teamwork.business.self.SelfInfoAct
import com.fcy.teamwork.databinding.ItemSelectMemberBinding
import com.fcy.teamwork.pojo.Person

class RecommendAdapter : BaseRVAdapter<Person, ItemSelectMemberBinding>() {
    private val selectedMembers = mutableListOf<Person>()
    fun getAllSelected(): List<Person> {
        return selectedMembers
    }

    override fun createBinding(
        inflater: LayoutInflater,
        parent: ViewGroup,
        viewType: Int
    ): ItemSelectMemberBinding {
        return ItemSelectMemberBinding.inflate(inflater)
    }

    override fun bindView(holder: VH, position: Int, data: Person) {
        (holder.binding as? ItemSelectMemberBinding)?.apply {
            tvNikeName.text = data.nikeName
            pvPortrait.setOnClickListener {
                Intent(root.context, SelfInfoAct::class.java).apply {
                    putExtra("canSenMsg", true)
                    putExtra("userName", data.personId)
                    putExtra("userInfo", data.toJson())
                    root.context.startActivity(this)
                }
            }
            cbSelect.setOnClickListener {
                it as CheckBox
                if (it.isChecked) {
                    if (!selectedMembers.contains(data)) {
                        selectedMembers.add(data)
                    }
                } else {
                    if (selectedMembers.contains(data)) {
                        selectedMembers.remove(data)
                    }
                }
            }
        }
    }
}